#include <iostream>
#include <future>
#include <cmath>

using namespace std;

static const int MAX = 10e8;
static double sum = 0;

void worker(int min, int max)
{
    cout << "worker!" << this_thread::get_id() << endl;
    for (int i = min; i <= max; i++)
    {
        sum += sqrt(i);
    }
}

class Worker
{
public:
    Worker(int min, int max)
        :m_min(min), m_max(max){}
    double work()
    {
        m_result = 0;
        for (int i = m_min; i < m_max; i++)
        {
            m_result += sqrt(i);
        }
        return m_result;
    }
    double getResult()
    {
        return m_result;
    }
private:
    int m_min;
    int m_max;
    double m_result;
};

int main()
{
    sum = 0;
    cout << "Hello World!" << this_thread::get_id() << endl;

    //auto f1 = async(worker, 0, MAX);
    cout << "Async task triggered" << endl;
    //f1.wait();
    //double result = 0;
    /*
    auto f2 = async(launch::async, [&result](){
        cout << "lambda task in thread:" << this_thread::get_id() << endl;
        for (int i = 0; i <= MAX; i++)
        {
            result += sqrt(i);
        }
    });
    f2.wait();
    */
    Worker w(0, MAX);
    auto f3 = async(&Worker::work, &w);
    f3.wait();
    cout << "Async task finish, result:" << w.getResult() << endl;
    return 0;
}
